{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-11-21T07:48:27.781058Z",
     "start_time": "2025-11-21T07:48:27.776925Z"
    }
   },
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "import torch.optim as optim\n",
    "\n",
    "# Distance in miles\n",
    "distance = torch.tensor([[1.0], [2.0], [3.0], [4.0]], dtype=torch.float32)\n",
    "\n",
    "# Time in minutes\n",
    "time = torch.tensor([[6.96], [12.11], [16.77], [22.21]], dtype=torch.float32)\n"
   ],
   "outputs": [],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-21T07:48:27.793558Z",
     "start_time": "2025-11-21T07:48:27.790446Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Define the model\n",
    "model = nn.Sequential(\n",
    "    nn.Linear(1, 1)\n",
    ")"
   ],
   "id": "eff8977d3cd9259",
   "outputs": [],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-21T07:48:27.805627Z",
     "start_time": "2025-11-21T07:48:27.801871Z"
    }
   },
   "cell_type": "code",
   "source": [
    "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
    "print(f\"Using device: {device}\")"
   ],
   "id": "493581fec5a9f2",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Using device: cuda\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-21T07:48:27.824363Z",
     "start_time": "2025-11-21T07:48:27.821013Z"
    }
   },
   "cell_type": "code",
   "source": "model = model.to(device)",
   "id": "4daee0338a2c10d9",
   "outputs": [],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-21T07:48:27.852187Z",
     "start_time": "2025-11-21T07:48:27.848470Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Define the loss function and optimizer\n",
    "loss_fn = nn.MSELoss()\n",
    "optimizer = optim.SGD(model.parameters(), lr=0.01)\n",
    "# optimizer = optim.Adam(model.parameters(), lr=0.0001)\n"
   ],
   "id": "d8a036e92c6ed2fd",
   "outputs": [],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-21T07:48:29.103572Z",
     "start_time": "2025-11-21T07:48:27.862976Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Train loop\n",
    "for epoch in range(2000):\n",
    "    # Reset optimizer\n",
    "    optimizer.zero_grad()\n",
    "\n",
    "    # Forward pass\n",
    "    distance = distance.to(device)\n",
    "    time = time.to(device)\n",
    "    y_pred = model(distance)\n",
    "\n",
    "    # Compute loss\n",
    "    loss = loss_fn(y_pred, time)\n",
    "\n",
    "    # Backward pass\n",
    "    loss.backward()\n",
    "\n",
    "    # Update weights\n",
    "    optimizer.step()\n",
    "\n",
    "    if epoch % 100 == 0:\n",
    "        print(f\"Epoch {epoch}: Loss = {loss.item()}\")\n"
   ],
   "id": "c081acbc311d4219",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 0: Loss = 293.892822265625\n",
      "Epoch 100: Loss = 0.052671730518341064\n",
      "Epoch 200: Loss = 0.04037969559431076\n",
      "Epoch 300: Loss = 0.03363151103258133\n",
      "Epoch 400: Loss = 0.029926978051662445\n",
      "Epoch 500: Loss = 0.02789296768605709\n",
      "Epoch 600: Loss = 0.026776352897286415\n",
      "Epoch 700: Loss = 0.02616341970860958\n",
      "Epoch 800: Loss = 0.02582690492272377\n",
      "Epoch 900: Loss = 0.025642231106758118\n",
      "Epoch 1000: Loss = 0.025540821254253387\n",
      "Epoch 1100: Loss = 0.025485198944807053\n",
      "Epoch 1200: Loss = 0.025454510003328323\n",
      "Epoch 1300: Loss = 0.0254378542304039\n",
      "Epoch 1400: Loss = 0.025428449735045433\n",
      "Epoch 1500: Loss = 0.025423645973205566\n",
      "Epoch 1600: Loss = 0.025420809164643288\n",
      "Epoch 1700: Loss = 0.025419343262910843\n",
      "Epoch 1800: Loss = 0.025418255478143692\n",
      "Epoch 1900: Loss = 0.025417856872081757\n"
     ]
    }
   ],
   "execution_count": 33
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-11-21T07:48:29.123476Z",
     "start_time": "2025-11-21T07:48:29.116677Z"
    }
   },
   "cell_type": "code",
   "source": [
    "model.eval()\n",
    "# Make Predictions\n",
    "with torch.no_grad():\n",
    "    test_distance = torch.tensor([1, 2, 3, 4, 25], dtype=torch.float32).unsqueeze(1)\n",
    "    test_distance = test_distance.to(device)\n",
    "    predictions = model(test_distance)\n",
    "    print(predictions)\n"
   ],
   "id": "2336a5ffb0da0b11",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[  6.9501],\n",
      "        [ 11.9916],\n",
      "        [ 17.0330],\n",
      "        [ 22.0745],\n",
      "        [127.9452]], device='cuda:0')\n"
     ]
    }
   ],
   "execution_count": 34
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
